package com.caiyouxi.advertise.controller;


import com.caiyouxi.advertise.model.Seat;
import com.caiyouxi.advertise.service.SeatService;
import com.caiyouxi.common.Constant;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.control.BaseController;
import com.caiyouxi.common.exception.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/seat")
public class SeatController extends BaseController {

    @Autowired
    SeatService seatService;

    /**
     * 获取所有位置
     */
    @RequestMapping("/getAll")
    public ResData get_all_seat(HttpServletResponse response, HttpServletRequest request, @RequestBody Seat bean){
        if(null == bean.getType()){ //1 代表主题 0 位置
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"type",null);
        }
        return seatService.get_all(bean);
    }

    /**
     * 获取主题列表
     */
    @RequestMapping("/get_list")
    public ResData get_list(HttpServletResponse response, HttpServletRequest request, @RequestBody Seat bean){
        if(null == bean.getType()){ //1 代表主题 0 位置
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"type",null);
        }
        return seatService.get_list(bean);
    }

    /**
     * 获取主题详情
     */
    @RequestMapping("/get_info")
    public ResData get_info(HttpServletResponse response, HttpServletRequest request, @RequestBody Seat bean){
        if(null == bean.getType()){ //1 代表主题 0 位置
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"type",null);
        }
        if(null == bean.getId()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"id",null);
        }
        return seatService.get_info(bean);
    }


    /**
     * 添加位置/
     * @param response
     * @param request
     * @param bean
     * @return
     */
    @RequestMapping("/add")
    public ResData add(HttpServletResponse response, HttpServletRequest request, @RequestBody Seat bean){

        if(null == bean.getType()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"type",null);
        }
        if(null == bean.getApp()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"app",null);
        }
        if(null == bean.getPage_name()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"page_name",null);
        }

        return seatService.add_seat(bean);
    }

    /**
     *删除位置
     */
    @RequestMapping("/delete")
    public ResData delete(HttpServletResponse response, HttpServletRequest request, @RequestBody Seat bean){
        if(null == bean.getId()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"id",null);
        }
        bean.setIs_delete(Constant.DELETED); //逻辑删除
        return seatService.delete_seat(bean);
    }

    /**
     *更新主题/位置 状态 排序
     */
    @RequestMapping("/update")
    public ResData update(HttpServletResponse response, HttpServletRequest request, @RequestBody Seat bean){
        if(null == bean.getId()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"id",null);
        }
        return seatService.update(bean);
    }



}
